﻿package model.drawPanel.entitys;

import java.awt.Polygon;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.swt.SWTResourceManager;


public class Line extends Entity {

  /**
	 * 
	 */
private static final long serialVersionUID = 101250102L;
private boolean isFirstPoint;

public Line(int id, Color color, int width, int style, int x1, int y1,
			int x2, int y2) {
		
		super(id, color, width, style, x1, y1, x2, y2);
		isFirstPoint = true;
		// TODO Auto-generated constructor stub
	}
public void draw(Display display ,GC gc) {
	Color backColor = gc.getBackground();
	Color foreColor = gc.getForeground();
	if(color ==null){
		super.initeColor();
	}
		if(isChoosed){
			gc.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
			gc.setLineWidth(3);
			gc.setLineStyle(SWT.LINE_SOLID);
			if(x2==x1){
				gc.drawOval(x1-6, Math.min(y1, y2)-12, 12, 12);
				gc.drawOval(x2-6, Math.max(y1, y2), 12, 12);
			}else{
				double k = (y2-y1)/(x2-x1 + 0.0);
				if( k < 0.1 && k>-0.1){
					gc.drawOval(x1-6, Math.min(y1, y2)-12, 12, 12);
					gc.drawOval(x2-6, Math.max(y1, y2), 12, 12);
					
				}else{
				double b = y1-k*x1;
				int xleft =(int) (Math.max(x1, x2)-6/(Math.sqrt(1 + k*k)) + 12/Math.sqrt(1+k*k));
				int xright =(int) (Math.min(x1, x2) + 6/(Math.sqrt(1 +k*k)) - 12/Math.sqrt(1 + k*k));
				gc.drawOval(xleft-6,(int)( k*xleft+b-6), 12, 12);
				gc.drawOval(xright-6,(int)(k*xright+b-6), 12, 12);
				}
			}
		 gc.setAlpha(128);
		 gc.setForeground(color);
		 gc.setLineStyle(style);
		 gc.setLineWidth(width);
		 gc.drawLine(x1, y1, x2, y2);
		 gc.setAlpha(255);
	}else{
		 gc.setForeground(color);
		 gc.setLineStyle(style);
		 gc.setLineWidth(width);
		 gc.drawLine(x1, y1, x2, y2);
	}
	 
	 gc.setBackground(backColor);
	 gc.setForeground(foreColor);
  }
@Override
public String isFocused(int x, int y) {
	Polygon p = new Polygon();
	addPoints(x1,y1,p);
	isFirstPoint = false;
	addPoints(x2,y2,p);
	isFirstPoint =true;
	if(p.contains(x, y)){
		return"entity";
	}
	if(isChoosed){
		if(distance(x,y,x1-6,y1-6) <= 6){
			return"zoomleftup";
		}
		if(distance(x,y,x2+6,y2+6) <= 6){
			return"zoomrightdown";
		}
	}
	return "noEnter";
}
private void addPoints(int x,int y,Polygon p){
	if(x2-x1==0){
		if(isFirstPoint){
		p.addPoint(x-width/2, y);
		p.addPoint(x+width/2, y);
		return;
		}else{
			p.addPoint(x+width/2, y);
			p.addPoint(x - width/2, y);
			return;
		}
	}
	double k = (-1.0 / ((y2-y1)/(x2-x1 + 0.0)));
	if( k < 0.1&& k> -0.1){
		if(isFirstPoint){
			p.addPoint(x-width/2, y);
			p.addPoint(x+width/2, y);
			return;
			}else{
				p.addPoint(x+width/2, y);
				p.addPoint(x - width/2, y);
				return;
			}
	}
	double b = y-k*x;
	double temp = Math.sqrt(width*width/(4*(1+k*k)));
	if(isFirstPoint){
		p.addPoint((int)(x + temp), (int)(k*(x +temp)+b));
		p.addPoint((int)(x - temp), (int)(k*(x -temp)+b));
	}else{
		p.addPoint((int)(x - temp), (int)(k*(x -temp)+b));
		p.addPoint((int)(x + temp), (int)(k*(x +temp)+b));
	}
}
}